<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_barang extends CI_Model {
	
    function __construct()
    {
        parent::__construct();
    }
    
	function input_barang()
	{
		$now = date("Y-m-d H-i-s");
		$data['Judul']        = $this->input->post("namabarang");
		$data['Deskripsi']    = $this->input->post("deskripsi");
		$data['Harga']        = $this->input->post("harga");
		$data['ID_Kategori']  = $this->input->post("kategori");
		$data['Tanggal']      = $now;
        $data['user_id']      = '1';
        $data['Image_Path']   = 'barang_'.$this->input->post("id");
		return $this->db->insert("jb_barang",$data);
	}
    
    function edit_barang($id)
    {
        $id = $this->input->post('idbarang');
        $edit_data =    array(
                        'Judul'         => $this->input->post('judul'),
                        'Deskripsi'     => $this->input->post('deskripsi'),
                        'Harga'         => $this->input->post('harga'),
                        );
        $this->db->where('ID_Barang',$id);
        $this->db->update('jb_barang',$edit_data);
    }
    
    function get_barang()
    {
        $this->db->select('*');
        $this->db->from('jb_barang');
        $this->db->join('jb_category','jb_barang.ID_Kategori = jb_category.ID_Kategori');
        $this->db->join('frum_meta','jb_barang.user_id = frum_meta.user_id');
        $this->db->order_by('jb_barang.Tanggal','DESC');
        $params = $this->db->get();
        return $params->result();
    }
    
    function detail_barang($id = '')
    {
        $this->db->select('*');
        $this->db->from('jb_barang');
        $this->db->join('jb_category','jb_barang.ID_Kategori = jb_category.ID_Kategori');
        $this->db->join('frum_meta','jb_barang.user_id = frum_meta.user_id');
        $this->db->where('jb_barang.ID_Barang',$id);
        $params = $this->db->get();
        return $params->row();
    }
    
    function get_last_post()
    {
        $this->db->order_by('Tanggal','DESC');
        $this->db->limit(6);    
        $params = $this->db->get('jb_barang');
        return $params->result();
    }
    
    function cari_barang()
    {
        $this->db->select('*');
        $this->db->from('jb_barang');
        $this->db->join('jb_category','jb_barang.ID_Kategori = jb_category.ID_Kategori');
        $this->db->like('Judul',$this->input->post('cari'));
        $this->db->or_like('Deskripsi',$this->input->post('cari'));
        $params = $this->db->get();
        return $params->result();
    }
    
    function get_id_barang($id)
    {
        $params = $this->db->get_where('jb_barang', array('ID_Barang'=>$id));
        return $params->row();
    }
    
    function get_last_id()
    {
        $this->db->select_max('ID_Barang');
        $params = $this->db->get('jb_barang');
        return $params->row();
    }
}